package top.tocchen.bookshop.controller.manager;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.tocchen.bookshop.entity.Response;
import top.tocchen.bookshop.entity.admin.ManagerUserEntity;
import top.tocchen.bookshop.enums.HttpStatusEnum;
import top.tocchen.bookshop.service.admin.impl.ManagerUserServiceImpl;
import top.tocchen.bookshop.util.ObjectUtil;

import javax.servlet.http.HttpSession;


/**
 * @author tocchen
 * @date 2022/8/10 11:09
 * @since jdk 1.8
 **/
@Controller
@RequestMapping("/admin")
public class ManagerLoginController {

    @Autowired
    ManagerUserServiceImpl userService;

    @GetMapping({"","/login"})
    public String loginPage(HttpSession session){
        if (session.getAttribute("token") != null){
            return "admin/home";
        }else {
            return "manager_login";
        }

    }

    @ResponseBody
    @PostMapping("/login/server")
    public Response<?> loginService(@RequestBody JSONObject json,HttpSession session){
        String account = json.getString("account");
        String password = json.getString("password");
        if (ObjectUtil.isEmpty(account) || ObjectUtil.isEmpty(password)){
            return Response.fail(HttpStatusEnum.FAIL_STATUS);
        }
        ManagerUserEntity managerUserEntity = userService.queryAccountInfo(account, password);
        if (ObjectUtil.isEmpty(managerUserEntity)){
            return Response.fail(HttpStatusEnum.FAIL_STATUS);
        }
        session.setAttribute("token",account+"tocchen"+password);
        return Response.success();
    }


}
